package com.next.aap.server.persistance.helper;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.next.aap.server.persistance.UserDevice;
import com.next.core.db.page.PageInfo;
import com.next.core.db.page.PageResult;
import com.next.core.db.page.QueryParam;
import com.next.core.db.validator.ObjectValidator;
import com.next.core.exception.AppException;
import com.next.ext.core.db.page.HibernateQueryParamPageInfo;
import com.next.ext.core.db.page.HibernateQueryParamPageInfo.ORDER;
import com.next.ext.core.persistance.dao.BasePersistanceDao;

@Component
public class UserDeviceHelper extends BasePersistanceDao<UserDevice> implements ObjectValidator<UserDevice> {

	private Logger logger = LoggerFactory.getLogger(this.getClass());
	/**
	 * Creates/updates a userDevice in Database
	 * 
	 * @param userDevice
	 * @return saved userDevice
	 * @throws AppException
	 */
	public UserDevice saveUserDevice(UserDevice userDevice) throws AppException
	{
		userDevice = super.saveObject(userDevice);
		return userDevice;
	}

	/**
	 * deletes a userDevice in Database
	 * 
	 * @param userDevice
	 * @return updated userDevice
	 * @throws AppException
	 */
	public void deleteUserDevice(UserDevice userDevice) throws AppException {
		super.deleteObject(userDevice);
	}

	/**
	 * return a UserDevice with given primary key/id
	 * 
	 * @param id
	 * @return UserDevice with PK as id(parameter)
	 * @throws AppException
	 */
	public UserDevice getUserDeviceById(Long id) throws AppException
	{
		UserDevice userDevice = super.getObjectByPK(UserDevice.class, id);
		return userDevice;
	}

	/**
	 * @param pageInfo
	 * @return search result
	 * @throws AppException
	 */
	public PageResult<UserDevice> searchUserDevices(PageInfo pageInfo) throws AppException
	{
		return super.findObject(UserDevice.class, pageInfo);
	}

	public List<UserDevice> getAllUserDevices() throws AppException
	{
		PageInfo pageInfo = null;
		return super.findObject(UserDevice.class, pageInfo).getResultList();
	}
	public UserDevice getUserDeviceByRegId(String deviceRegId) throws AppException{
		logger.info("getUserDeviceByRegId  {}",deviceRegId);
		HibernateQueryParamPageInfo pageInfo = new HibernateQueryParamPageInfo();
		QueryParam emailQueryParam = new QueryParam();
		emailQueryParam.setCaseSenstive(false);
		emailQueryParam.setField("regId");
		emailQueryParam.setFieldType(QueryParam.FIELD_TYPE_STRING);
		emailQueryParam.setOperator(QueryParam.OPERATOR_EQUAL);
		emailQueryParam.setValue(deviceRegId);
		pageInfo.addCriteria(emailQueryParam);
		
		PageResult<UserDevice> pr = this.searchUserDevices(pageInfo);
		if(pr.getResultList().size() <= 0){
			return null;
		}
		return pr.getResultList().get(0);
	}
	public List<UserDevice> getAndroidUserDeviceAfterId(Long id, int pageSize) throws AppException{
		logger.info("getUserDeviceAfterId  {}",id);
		HibernateQueryParamPageInfo pageInfo = new HibernateQueryParamPageInfo();
		QueryParam emailQueryParam = new QueryParam();
		emailQueryParam.setCaseSenstive(false);
		emailQueryParam.setField("id");
		emailQueryParam.setFieldType(QueryParam.FIELD_TYPE_NUMBER);
		emailQueryParam.setOperator(QueryParam.OPERATOR_MORE_THEN);
		emailQueryParam.setValue(id);
		pageInfo.addCriteria(emailQueryParam);
		
		QueryParam deviceTypeQueryParam = new QueryParam();
		deviceTypeQueryParam.setCaseSenstive(false);
		deviceTypeQueryParam.setField("deviceType");
		deviceTypeQueryParam.setFieldType(QueryParam.FIELD_TYPE_STRING);
		deviceTypeQueryParam.setOperator(QueryParam.OPERATOR_EQUAL);
		deviceTypeQueryParam.setValue("Android");
		pageInfo.addCriteria(deviceTypeQueryParam);
		
		pageInfo.setPageSize(pageSize);
		
		pageInfo.addOrderBy("id", ORDER.ASC);
		
		PageResult<UserDevice> pr = this.searchUserDevices(pageInfo);
		return pr.getResultList();
	}
	

	public void validateObjectForCreate(UserDevice userDevice) throws AppException {
		System.out.println("validateObjectForCreate");
		checkIfStringMissing("Device Type", userDevice.getDeviceType());
		checkIfStringMissing("Registration Id", userDevice.getRegId());
	}

	public void validateObjectForUpdate(UserDevice userDevice) throws AppException {
		checkIfStringMissing("Device Type", userDevice.getDeviceType());
		checkIfStringMissing("Registration Id", userDevice.getRegId());
	}

}